

var o = require("wsToolsJS"),
    i = require("MtilsJS"),
    a = cc.Class({
        // extends: cc.Component,
        properties: {
            send_count: 0,
            recv_count: 0,
            player_id: "",
            ws_url: "",
            wxTool: 0,
            sendGetCallback: null,
            sendGetArchiveCallback: null,
            sendGetUtcCallback: null,
            sendGetConfigCallback: null,
            sendGetOnlineGameListCallback: null,
            sendGetUserInfoCallback: null,
            sendPostCallback: null,
            sendPostArchiveCallback: null,
            sendPostConfigCallback: null,
            sendPostUserInfoCallback: null,
            sendAppendCallback: null,
            sendAppendArchiveCallback: null,
            sendAppendFriendHelpCallback: null,
            loginCallback: null,
            onCloseCallback: null,
            errorMsgCallback: null
        },
        ctor: function () {
            this.wxTool = new o("Star", "小游戏APPID");
        },
        initWebsocketBindFun: function (t) {
            0 == t.runMode ? this.ws_url = "wss://www.7cgames.cn:8700/" : 1 == t.runMode ? this.ws_url = "ws://192.168.0.108:8700/" : this.ws_url = "ws://192.168.0.108:8702/",
                this.player_id = t.playerId, this.loginCallback = t.loginFun, this.onCloseCallback = t.onCloseFun,
                this.initWebsocket();
        },
        SetGameID: function (t, e) {
            this.wxTool = new o(t, e);
        },
        SendGetGameArchiveBindFun: function (t) {
            this.SendGetRequestBindFun({
                key_word: "ws_archive",
                sendGetFun: t.sendGetFun
            });
        },
        SendPostGameArchiveBindFun: function (t) {
            this.SendPostRequestBindFun({
                key_word: "ws_archive",
                val_word: String(t.valWord),
                sendPostFun: t.sendPostFun
            });
        },
        SendAppendGameArchiveBindFun: function (t) {
            this.SendAppendRequestBindFun({
                key_word: "ws_archive",
                val_word: String(t.valWord),
                sendGetFun: t.sendGetFun
            });
        },
        SendClearGameArchiveBindFun: function (t) {
            this.SendPostRequestBindFun({
                key_word: "ws_archive",
                val_word: "",
                sendPostFun: t.sendPostFun
            });
        },
        SendPostUserInfoBindFun: function (t) {
            var e = this.wxTool.GetUserInfoJsonStr(t.OpenId, t.NickName, t.AvatarUrl);
            this.SendPostRequestBindFun({
                key_word: "ws_post_user_info",
                val_word: e,
                sendPostFun: t.sendPostFun
            });
        },
        SendGetUserInfoByOpenIdBindFun: function (t) {
            var e = this.wxTool.CalPlayerEncryptId(t.openId);
            this.SendGetRequestBindFun({
                key_word: "ws_get_user_info",
                open_id: e,
                sendGetFun: t.sendGetFun
            });
        },
        SendGetFriendHelpBindFun: function (t) {
            this.SendGetRequestBindFun({
                key_word: "ws_friend_help",
                sendGetFun: t.sendGetFun
            });
        },
        SendPostFriendHelpBindFun: function (t) {
            this.SendPostRequestBindFun({
                key_word: "ws_friend_help",
                val_word: String(t.valWord),
                sendPostFun: t.sendPostFun
            });
        },
        SendClearFriendHelpBindFun: function (t) {
            this.SendPostRequestBindFun({
                key_word: "ws_friend_help",
                val_word: "",
                sendPostFun: t.sendPostFun
            });
        },
        SendAppenFriendHelpBindFun: function (t) {
            this.SendAppendShareBindFun({
                shareUserId: t.shareUserId,
                key_word: "ws_friend_help",
                val_word: String(t.valWord),
                sendAppendFun: t.sendAppendFun
            });
        },
        SendGetServerUtcBindFun: function (t) {
            this.SendGetRequestBindFun({
                key_word: "ws_get_utc_str",
                sendGetFun: t.sendGetFun
            });
        },
        SendGetConfigBindFun: function (t) {
            this.SendGetRequestBindFun({
                key_word: "ws_get_config",
                sendGetFun: t.sendGetFun
            });
        },
        initWebsocket: function () {
            var t = this;
            this._wsiSendBinary || (this._wsiSendBinary = new WebSocket(this.ws_url), this._wsiSendBinary.binaryType = "arraybuffer",
                console.log("pre initWebsocket url=" + this.ws_url + " user=" + this.player_id),
                this._wsiSendBinary.onopen = function (e) {
                    console.log("  websocket onopen   ");
                    var n = t.wxTool.GetLoginRequestStr(t.player_id);
                    console.log(" send login_request = " + n), t.sendWebSocketBinary(n);
                }, this._wsiSendBinary.onmessage = function (e) {
                    for (var n = new Uint16Array(e.data), o = "", i = 0; i < n.length; i++)
                        if (0 === n[i]) o += " ";
                        else {
                            var a = "0x" + n[i].toString("16").toUpperCase();
                            o += String.fromCharCode(a);
                        }
                    t.receiveWebSocketMsg(o);
                }, this._wsiSendBinary.onerror = function (t) {
                    console.log("wsiSendBinary onerror " + t);
                }, this._wsiSendBinary.onclose = function (e) {
                    console.log("wsiSendBinary onclose callback " + e.reason), t.onCloseCallback(e.reason),
                        t._wsiSendBinary = null;
                });
        },
        sendWebSocketBinary: function (t) {
            if (this._wsiSendBinary)
                if (this._wsiSendBinary.readyState === WebSocket.OPEN) {
                    var e = t;
                    if (e.length > this.wxTool.msg_max_len) return void console.log(" error length> " + this.wxTool.msg_max_len);
                    for (var n = new Uint16Array(e.length), o = 0; o < e.length; o++) n[o] = e.charCodeAt(o);
                    this._wsiSendBinary.send(n.buffer), ++this.send_count;
                } else {
                    console.log(" wsiSendBinary  error send binary websocket instance wasn't ready..."),
                        this.scheduleOnce(function () {
                            this.sendWebSocketBinary(t);
                        }, 1);
                }
            else console.log(" this._wsiSendBinary error " + this._wsiSendBinary);
        },
        receiveWebSocketMsg: function (t) {
            if ("{" == (t = t.trim())[0] && "}" == t[t.length - 1]) {
                var e = JSON.parse(t);
                if (e.status >= 0)
                    if (e.msgType == this.wxTool.msg_type_login) {
                        var n = e.msg;
                        console.log("login success, get vrsion:" + e.request), this.wxTool.SetTokenSeed(n),
                            this.loginCallback && this.loginCallback();
                    } else if (e.msgType == this.wxTool.msg_type_get)
                    if ("ws_archive" == e.request) this.sendGetArchiveCallback && this.sendGetArchiveCallback(e.msg, t);
                    else if ("ws_get_utc_str" == e.request) this.sendGetUtcCallback && this.sendGetUtcCallback(e.msg, t);
                else if ("ws_get_config" == e.request) this.sendGetConfigCallback && this.sendGetConfigCallback(e.msg, t);
                else if ("ws_get_user_info" == e.request) {
                    if (this.sendGetUserInfoCallback) {
                        var o = JSON.parse(e.msg);
                        o.avatarUrl = i.base64_decode(o.avatarUrl), o.nickName = i.base64_decode(o.nickName),
                            this.sendGetUserInfoCallback(o, t);
                    }
                } else this.sendGetCallback && this.sendGetCallback(e.msg, t);
                else e.msgType == this.wxTool.msg_type_post ? "ws_archive" == e.request ? this.sendPostArchiveCallback && this.sendPostArchiveCallback(e.msg, t) : "ws_post_user_info" == e.request ? this.sendPostUserInfoCallback && this.sendPostUserInfoCallback(e.msg, t) : this.sendPostCallback && this.sendPostCallback(e.msg, t) : e.msgType == this.wxTool.msg_type_append ? "ws_archive" == e.request ? this.sendAppendArchiveCallback && this.sendAppendArchiveCallback(e.msg, t) : "ws_friend_help" == e.request ? this.sendAppendFriendHelpCallback && this.sendAppendFriendHelpCallback(e.msg, t) : this.sendAppendCallback && this.sendAppendCallback(e.msg, t) : console.log("recv unkon type : " + e.msgType + " ," + e.msg);
                else console.log("recv error msg: status=" + e.status + ",msg=" + e.msg);
            } else console.log("recv Msg not json:" + t);
            ++this.recv_count;
        },
        closeWebSocket: function () {
            if (this._wsiSendBinary && this._wsiSendBinary.readyState !== WebSocket.CLOSED && this._wsiSendBinary.readyState === WebSocket.OPEN) {
                console.log("pre websocket close "), this._wsiSendBinary.close(1e3, "Normal Closure");
            }
        },
        SendGetRequest: function (t) {
            var e = String(t);
            if (e.length > 0) {
                if (0 == e.indexOf("ws_")) var n = this.wxTool.GetGetValRequestStr(this.player_id, e);
                this.sendWebSocketBinary(n);
            }
        },
        SendGetRequestBindFun: function (t) {
            var e = String(t.key_word);
            if (e.length > 0) {
                if ("ws_get_user_info" == e) {
                    var n = this.wxTool.GetGetOpenidValRequestStr(this.player_id, e, t.open_id);
                    console.log("GetGetOpenidValRequestStr :" + n);
                } else if (0 == e.indexOf("ws_")) n = this.wxTool.GetGetValRequestStr(this.player_id, e);
                else console.log("msg_key error  " + e);
                this.sendWebSocketBinary(n), "ws_archive" == e ? this.sendGetArchiveCallback = t.sendGetFun : "ws_get_utc_str" == e ? (this.sendGetUtcCallback = t.sendGetFun,
                    console.log(" UTC pre send  = " + n)) : "ws_get_config" == e ? this.sendGetConfigCallback = t.sendGetFun : "ws_get_user_info" == e ? this.sendGetUserInfoCallback = t.sendGetFun : this.sendGetCallback = t.sendGetFun;
            }
        },
        SendPostRequest: function (t, e) {
            var n = String(t),
                o = String(e);
            if (n.length > 0) {
                if (0 == n.indexOf("ws_")) var i = this.wxTool.GetPostValRequestStr(this.player_id, n, o);
                this.sendWebSocketBinary(i);
            }
        },
        SendPostRequestBindFun: function (t) {
            var e = String(t.key_word),
                n = String(t.val_word);
            if (e.length > 0) {
                if (0 == e.indexOf("ws_")) var o = this.wxTool.GetPostValRequestStr(this.player_id, e, n);
                this.sendWebSocketBinary(o), "ws_archive" == e ? this.sendPostArchiveCallback = t.sendPostFun : "ws_post_user_info" == e ? this.sendPostUserInfoCallback = t.sendPostFun : this.sendPostCallback = t.sendPostFun;
            }
        },
        SendAppendRequest: function (t, e) {
            var n = String(t),
                o = String(e);
            if (n.length > 0) {
                if (console.log(" pre send  lab = " + n), 0 == n.indexOf("ws_")) {
                    var i = this.wxTool.GetPostAppendValRequestStr(this.player_id, n, o);
                    console.log(" pre send  GetValRequest = " + i);
                }
                this.sendWebSocketBinary(i);
            }
        },
        SendAppendRequestBindFun: function (t) {
            var e = String(t.key_word),
                n = String(t.val_word);
            if (e.length > 0) {
                if (0 == e.indexOf("ws_")) var o = this.wxTool.GetPostAppendValRequestStr(this.player_id, e, n);
                this.sendWebSocketBinary(o), "ws_archive" == e ? this.sendAppendArchiveCallback = t.sendAppendFun : this.sendAppendCallback = t.sendAppendFun;
            }
        },
        SendShareAppendRequest: function (t, e) {
            var n = "ws_friend_help",
                o = String(e);
            if (n.length > 0) {
                if (console.log(" pre send  lab = " + n), 0 == n.indexOf("ws_")) {
                    var i = this.wxTool.GetPostShareAppendValRequestStr(this.player_id, t, n, o);
                    console.log(" pre send  GetValRequest = " + i);
                }
                this.sendWebSocketBinary(i);
            }
        },
        SendAppendShareBindFun: function (t) {
            var e = "ws_friend_help",
                n = String(t.val_word),
                o = String(t.shareUserId);
            if (e.length > 0) {
                if (console.log(" pre send  lab = " + e), 0 == e.indexOf("ws_")) {
                    var i = this.wxTool.GetPostShareAppendValRequestStr(this.player_id, o, e, n);
                    console.log(" pre send  GetValRequest = " + i);
                }
                this.sendWebSocketBinary(i), this.sendAppendFriendHelpCallback = t.sendAppendFun;
            }
        }
    });
module.exports = a